Page({
  data: {
    roomId: ''
  },
  onScanCode() {
    wx.scanCode({
      success: (res) => {
        this.setData({ roomId: res.result });
      }
    });
  },
  onJoinRoom() {
    if (!this.data.roomId) {
      wx.showToast({ title: '请输入房间号', icon: 'none' });
      return;
    }
    // 云开发写入playerB信息
    if (wx.cloud && this.data.userInfo) {
      const db = wx.cloud.database();
      db.collection('rooms').doc(this.data.roomId).update({
        data: {
          playerB: this.data.userInfo,
          playerBReady: true
        }
      });
    }
    wx.navigateTo({ url: `/pages/score/index?roomId=${this.data.roomId}` });
  }
});
